<?php
class Default_Model_Congno extends Henry_Db_Table {
	static private $TBNAME = "congno";
	protected $_name = "congno"; 
  	protected $_primary = array(
  							'customer_id',
  							'ngay'
  	); 
  	public function init()
  	{
  		$this->_name = parent::PREFIX.$this->_name;
  	}
  	private function checkExists($CusId,$Ngay)
  	{
  		$select = $this->_db->select();
		$select->from($this->_name,array('customer_id'))
		       ->where('customer_id = ?',$CusId)
		       ->where('ngay = ?',$Ngay);
		$data = $this->_db->fetchAll($select);
		if (empty($data)) return false;
		return true;
  	} 
  	private function chinhsuaCongNo($data)
  	{
  		//Chuan bi du lieu chinh sua.
  		$sqlData = array(
			'customer_id' => $data['customer_id'],
			'ngay'  => $data['ngay'],
			'ghichu'=> $data['ghichu']
		);
		//Du lieu trong DB
		$where[] = $this->_db->quoteInto("customer_id = ?",$sqlData['customer_id']);
		$where[] = $this->_db->quoteInto("ngay = ?",$sqlData['ngay']);
		$dataInDB = $this->fetchRow($where)->toArray();
		
		$SoTienCapNhap = 0;
		if (empty($data['sotien_chieu'])) $data['sotien_chieu'] = 0;
		
		$sqlData['sotien_chieu'] = (double)$data['sotien_chieu'];
		$SoTienCapNhap = $SoTienCapNhap + $sqlData['sotien_chieu'] - $dataInDB['sotien_chieu'];

		if (empty($data['sotien_hn'])) $data['sotien_hn'] = 0;
		$sqlData['sotien_hn'] = (double)$data['sotien_hn'];
		$SoTienCapNhap = $SoTienCapNhap + $sqlData['sotien_hn'] - $dataInDB['sotien_hn'];

		$tongCn = new Default_Model_TongCongNo();
		$tongTienCu = $tongCn->layTongTien($sqlData['customer_id']);
		//Cap Nhap Lai Tong Tien Cong No	
		$SoTienCapNhap = $tongTienCu + $SoTienCapNhap;
		$tongCn->suaTongSoTien($sqlData['customer_id'],$SoTienCapNhap);
		//Cap Nhap lai cong no
		$this->update($sqlData,$where);
  	}
	public function luuCongno($data)
	{
		//Chuan bi data dua vao DB
		$sqlData = array(
			'customer_id' => $data['customer_id'],
			'ngay'  => $data['ngay'],
			'ghichu'=> $data['ghichu']
		);
		if (empty($data['sotien_chieu'])) $data['sotien_chieu'] = 0;
		$sqlData['sotien_chieu'] = (double)$data['sotien_chieu'];
		if (empty($data['sotien_hn'])) $data['sotien_hn'] = 0;
		$sqlData['sotien_hn'] = (double)$data['sotien_hn'];
		$tongNguyenNgay = $sqlData['sotien_chieu']+$sqlData['sotien_hn'];
		//Khai bao transaction
		$db = $this->_db;
		$db->beginTransaction();
		try {
			if ($this->checkExists($sqlData['customer_id'],$sqlData['ngay']))
			{
				$this->chinhsuaCongNo($sqlData);
			}
			else
			{
				$this->insert($sqlData);					
				$tongCn = new Default_Model_TongCongNo();
				if ($tongCn->tontaiCongNo($sqlData['customer_id']))
				{
					$TongTienCongNo = $tongCn->layTongTien($sqlData['customer_id']);
					$TongTienCongNo = $TongTienCongNo + $tongNguyenNgay;
				}
				else
				{
					$TongTienCongNo = $tongNguyenNgay;
				}
				//Cap Nhap Lai Tong Tien Cong No	
				$tongCn->capnhapCongNo($sqlData['customer_id'],$tongNguyenNgay);
			}
	    	$db->commit();
	    	return true;
		} 
		catch (Exception $e) {
		    $db->rollBack();
		    echo $e->getMessage();
		    return false;
		}
	}
	public function lietke($date)
	{
		if (!($date instanceof Zend_Date))
		{
			$date = new Zend_Date($date);
		}

		$select = $this->_db->select();
		$select ->from(array('c'=>Default_Model_Customer::getTBName()),array('customer_fname','customer_lname','customer_id'))
				->joinLeft(array('cn'=>$this->_name),"c.customer_id = cn.customer_id and cn.ngay = '{$date->get('Y-M-d')}'",array('sotien_chieu','sotien_hn'))
				->joinLeft(array('cnt'=>Default_Model_TongCongNo::getTBName()),'cnt.customer_id = c.customer_id',array('tongtien'))
 			    ->where("c.customer_status = ?",Default_Model_Customer::NORMAL);
 		
 		$data =  $this->_db->fetchAll($select,null,Zend_Db::FETCH_ASSOC);
 		$thuchiOb = new Default_Model_ThuChi();
 		$tcnOb = new Default_Model_TongCongNo();
 		$TongChieu = 0;
 		$TongHN = 0;
 		$TongCongNo = 0;
 		$TongThuChi = 0;
 		foreach ($data as $key => $item)
 		{
 			$data[$key]['khoangthuchi'] = $thuchiOb->layTongThuChitheoNgay($item['customer_id'],$date);
 			$data[$key]['tongtrongngay'] = $item['sotien_chieu'] + $item['sotien_hn'];
 			$TongChieu = $TongChieu + $item['sotien_chieu']; 
 			$TongHN    = $TongHN + $item['sotien_hn']; 
 			$TongCongNo = $TongCongNo + $tcnOb->layTongTien($item['customer_id']);
 			$TongThuChi = $TongThuChi + $data[$key]['khoangthuchi'];
 		}
 		$data['tongchieu'] = $TongChieu;
 		$data['tonghn']    = $TongHN;
 		$data['tongcangay']= $TongChieu+$TongHN;
 		$data['tongcongno']= $TongCongNo;
 		$data['tongthuchi']= $TongThuChi;
 		return $data;   
	}
	public function laySoTien($cusID,$ngay)
	{
		$select = $this->_db->select();
		$select->from(array($this->_name),array('sotien_chieu','sotien_hn'))
			   ->where("customer_id = ?",$cusID)
			   ->where("ngay = ?",$ngay->get("Y-M-d"))
		;
		return $this->_db->fetchRow($select);
	}
	public static function getTBName()
	{
		return parent::PREFIX.self::$TBNAME; 
	}
	public function xoaCongNobyCusId($id)
	{
		$where = $this->_db->quoteInto("customer_id = ?",$id);
		return $this->delete($where);	
	}
}
?>